﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CashBankIn.CoaServices;
using CashBankIn.JurnalServices;
using Lib.Helper;
using System.ComponentModel;
using MSIACCSL2.Command;
using WcfService1.DTO;
using System.Linq;

namespace CashBankIn.ViewModels
{
    public class RekonViewModel : ViewModelBase
    {
        private ObservableCollection<RekonModel> _jurnals;
        private JurnalServicesClient _jurnalClient;
        private CoaServicesClient coaClient;
        private DateTime _Tgl;
        private decimal _SaldoAkhir;
        private CoaDTO _AccountCashBank;
        private ObservableCollection<CoaDTO> _Coas;

        public RekonViewModel()
        {
            if (! DesignerProperties.IsInDesignTool)
            {
                _jurnalClient = new JurnalServicesClient();
                coaClient = new CoaServicesClient();
                GetJurnalCommand = new DelegateCommand(GetJurnal, CanGetJurnal);
                GetCoaKasBank();
            }
        }



          public ObservableCollection<RekonModel> Jurnals
        {
            get { return _jurnals; }
            set
            {
                if (ReferenceEquals(_jurnals,value) || value == null) return;
                _jurnals = value;
                FirePropertyChanged("Jurnals");
            }
        }


          public ObservableCollection<CoaDTO> Coas
          {
              get { return _Coas; }
              set
              {
                  if (ReferenceEquals(_Coas, value) || value == null) return;
                  _Coas = value;
                  FirePropertyChanged("Coas");
              }
          }

        public DateTime Tgl
        {
            get { return _Tgl; }
            set
            {
                if (Tgl == value) return;
                _Tgl = value;
                FirePropertyChanged("Tgl");
            }
        }

        public CoaDTO AccountCashBank
        {
            get { return _AccountCashBank; }
            set
            {
                if (ReferenceEquals(AccountCashBank,value) || value == null)return;
                _AccountCashBank = value;
                FirePropertyChanged("AccountCashBank");
            }
        }


        public Decimal SaldoAkhir
        {
            get { return _SaldoAkhir; }
            set
            {
                if (SaldoAkhir == value) return;
                _SaldoAkhir = value;
                FirePropertyChanged("SaldoAkhir");
            }
        }

        public ICommand GetJurnalCommand { get; set; }
        public Boolean CanGetJurnal(Object param)
        {
            return true;
        }


        public void GetCoaKasBank()
        {
            coaClient.GetCoaKasBankCompleted += coaClient_GetCoaKasBankCompleted;
            coaClient.GetCoaKasBankAsync();
        }

        void coaClient_GetCoaKasBankCompleted(object sender, GetCoaKasBankCompletedEventArgs e)
        {
            coaClient.GetCoaKasBankCompleted -= coaClient_GetCoaKasBankCompleted;
            Coas = e.Result;
        }


        public void GetJurnal(Object param)
        {
            _jurnalClient.GetListByPeriodeAndAkunCompleted += _jurnalClient_GetListByPeriodeAndAkunCompleted;
            _jurnalClient.GetListByPeriodeAndAkunAsync(Tgl,AccountCashBank);
        }

        void _jurnalClient_GetListByPeriodeAndAkunCompleted(object sender, GetListByPeriodeAndAkunCompletedEventArgs e)
        {
            _jurnalClient.GetListByPeriodeAndAkunCompleted -= _jurnalClient_GetListByPeriodeAndAkunCompleted;
            Jurnals = new ObservableCollection<RekonModel>();
            foreach (var jurnalDto in e.Result)
            {
                foreach (var jurnalDetailDto in jurnalDto.JurnalDetails)
                {
                    var rekon = new RekonModel();
                    rekon.Tanggal = jurnalDto.TglJurnal;
                    rekon.NoVoucher = jurnalDto.NoVoucher;
                    rekon.Description = jurnalDto.Description;
                    if (jurnalDetailDto.Debit > 0)
                        rekon.Debit = jurnalDetailDto.Debit;
                    if (jurnalDetailDto.Kredit > 0)
                        rekon.Kredit = jurnalDetailDto.Kredit;
                    Jurnals.Add(rekon);
                }

               
            }
        }
    }

    public class RekonModel : ViewModelBase
    {
        private DateTime _Tanggal;
        private string _NoVoucher;
        private string _Description;
        private decimal _Debit;
        private decimal _Kredit;
        private bool _IsMatch;

        public DateTime Tanggal
        {
            get { return _Tanggal; }
            set
            {
                if (value == Tanggal)return;
                _Tanggal = value;
                FirePropertyChanged("Tanggal");
            }
        }

        public string NoVoucher
        {
            get { return _NoVoucher; }
            set
            {
                if (value == NoVoucher) return;
                _NoVoucher = value;
                FirePropertyChanged("NoVoucher");
            }
        }

        public string Description
        {
            get { return _Description; }
            set
            {
                if (value == Description) return;
                _Description = value;
                FirePropertyChanged("Description");
            }
        }

        public Decimal Debit
        {
            get { return _Debit; }
            set
            {
                if (value == Debit) return;
                _Debit = value;
                FirePropertyChanged("Debit");
            }
        }

        public Decimal Kredit
        {
            get { return _Kredit; }
            set
            {
                if (value == Kredit) return;
                _Kredit = value;
                FirePropertyChanged("Kredit");
            }
        }

        public bool IsMatch
        {
            get { return _IsMatch; }
            set
            {
                if (value == IsMatch) return;
                _IsMatch = value;
                FirePropertyChanged("IsMatch");
            }
        }


    }
   
}

